package system

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// 角色和菜单关联表
type SysRoleMenu struct {
	RoleMenuId int64     `orm:"column(role_menu_id);pk"`
	RoleId     int64     `description:"角色ID"`
	MenuId     int64     `description:"菜单ID"`
	CreateTime time.Time `description:"创建时间"`
	ModifyTime time.Time `description:"最后更新时间"`
}

// 批量新增
func (rm *SysRoleMenu) AddMulti(list []*SysRoleMenu) (err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(list), list)
	return
}

//
//// 删除
func (rm *SysRoleMenu) DeleteByCondition(condition string, pars []interface{}) (err error) {
	if condition == "" {
		return err
	}
	o := orm.NewOrm()
	sql := ` DELETE FROM sys_role_menu WHERE `
	sql += condition
	_, err = o.Raw(sql, pars).Exec()
	return
}

func (rm *SysRoleMenu) GetMenuListByCondition(condition string, pars []interface{}) (results []*SysRoleMenu, err error) {
	o := orm.NewOrm()
	sql := `SELECT rm.* FROM sys_role_menu AS rm INNER JOIN  sys_menu r ON rm.menu_id = r.menu_id WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&results)
	return
}
